package io.renren.project.hongchang.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.renren.modules.wyzs.mobile_BillAdapterMoudle.*;
import io.renren.modules.wyzs.mobile_moudle.SalesOrderEntity;
import io.renren.modules.wyzs.service.FactoryService;
import io.renren.modules.wyzsphone.commWithWCF.BillTempGeter;
import io.renren.modules.wyzsphone.commWithWCF.CommWithWCF;
import io.renren.modules.wyzsphone.commWithWCF.XModeRtn;
import io.renren.modules.wyzsphone.commWithWCF.XModeSnd;
import io.renren.modules.wyzsphone.utils.Utils;
import io.renren.project.hongchang.service.HongchangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HongchangServiceImpl implements HongchangService {

    @Autowired
    FactoryService factoryService;

    //获取授权单的列表
    @Override
    public Map getAllTask(Map<String, Object> params) {
            Gson _gson = new Gson();
            CommWithWCF ser = new CommWithWCF();
            XModeSnd Value = new XModeSnd();
            BillTempGeter billTempGeter=new BillTempGeter();
            String url=factoryService.getUrlById((String)params.get("factoryId"));
            Value.setURL(url);
            Value.setFile("X20");//todo
            Value.setMethod("W_GetSalesEmpowerList");//W_GetSalesEmpowerList M_GetSalesTaskCar
            Value.setUID((String) params.get("uid"));
            Value.getData().setData("type", "AllSalesTaskInfo");
            Value.getData().setData("currentpage", params.get("currentpage".toString()));//todo

            BillTempGeter geter = new BillTempGeter();
            geter.setClassType(billTempGeter.getClassType());
            Value.setJsonObject(_gson.toJson(geter));
            XModeRtn ValueRtn = ser.Code(Value);
            List list= Utils.JsonToList(ValueRtn.getJsonObject());
            Map maps=new HashMap();
            maps.put("list",list);
            maps.put("state",ValueRtn.getMessage());
            return maps;
    }

    //获取账户列表
    @Override
    public Map getAllOrder(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X20");//todo
        Value.setMethod("W_GetSalesAccountList");//M_GetSalesAccountList  M_GetSalesOrder
        Value.setUID((String) params.get("uid"));
        String itemid=(String)params.get("itemid");
        if(itemid!=null&&itemid.equals("")) {
            Value.getData().setData("itemid", itemid);
        }else {
            Value.getData().setData("itemid", "");
        }
        String itemtype=(String)params.get("itemtype");
        if(itemid!=null&&itemid.equals("")) {
            Value.getData().setData("itemtype", itemtype);
        }else {
            Value.getData().setData("itemtype", "");
        }
        Value.getData().setData("currentpage", (String)params.get("currentpage"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=Utils.JsonToList(ValueRtn.getJsonObject());
        Map map=new HashMap();
        map.put("list",list);
        map.put("state",ValueRtn.getState());
        return map;
    }


    //获取某个授权单
    @Override
    public Map getSalesBill(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X20");//todo
        Value.setMethod("W_GetSalesBill");//W_GetSalesEmpowerList M_GetSalesTaskCar
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("billid", (String)params.get("ID"));
        Value.getData().setData("itemid", (String)params.get("ItemID"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
      //  List list= Utils.JsonToList(ValueRtn.getJsonObject());
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
       // maps.put("list",list);
        if(maps==null){
             maps=new HashMap();
        }
        maps.put("state",ValueRtn.getState());
        return maps;
    }

    //根据物料获取账户列表
    public Map getSalesAccountListByItemId(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X20");//todo
        Value.setMethod("W_GetSalesAccountListByItemID");//W_GetSalesEmpowerList M_GetSalesTaskCar
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("billid", (String)params.get("ID"));
        Value.getData().setData("itemid", (String)params.get("ItemID"));
        Utils.setDoubleToInt(params,"organizeId");
        Value.getData().setData("organizeid", (Integer)params.get("organizeId"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list= Utils.JsonToList(ValueRtn.getJsonObject());
        // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
        maps.put("state",ValueRtn.getState());
        maps.put("list",list);
        return maps;
    }

    //根据物料获取账户列表
    public Map getSalesAccountListByItemIdX05(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");//todo
        Value.setMethod("GetSalesAccountListByItemID");//W_GetSalesEmpowerList M_GetSalesTaskCar
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("billid", (String)params.get("ID"));
        Value.getData().setData("itemid", (String)params.get("ItemID"));
        Utils.setDoubleToInt(params,"organizeId");
        Value.getData().setData("organizeid", (Integer)params.get("organizeId"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list= Utils.JsonToList(ValueRtn.getJsonObject());
        // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
        maps.put("state",ValueRtn.getState());
        maps.put("list",list);
        return maps;
    }

    //给批量下单增加车辆
    @Override
    public Map AppCarListAdd(Map<String, Object> params,String jsonStr) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");//todo
        Value.setMethod("AppCarListAdd");//W_GetSalesEmpowerList M_GetSalesTaskCar
        Value.setJsonObject(jsonStr);
        Value.setUID((String) params.get("uid"));
        Utils.setDoubleToInt(params,"BillID");
        Value.getData().setData("BillID", (Integer)params.get("BillID"));
        Value.getData().setData("CarNo", (String)params.get("CarNo"));
        Value.getData().setData("Qty", (String)params.get("Qty"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        XModeRtn ValueRtn = ser.Code(Value);
        // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
        maps.put("state",ValueRtn.getState());
        return maps;
    }

    @Override
    public Map AppCarListDel(Map<String, Object> params, String jsonStr) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");//todo
        Value.setMethod("AppCarListDel");//W_GetSalesEmpowerList M_GetSalesTaskCar
        //Value.setJsonObject(jsonStr);
        Value.setUID((String) params.get("uid"));
        Utils.setDoubleToInt(params,"RowID");
        Value.getData().setData("rowid", (Integer)params.get("RowID"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        XModeRtn ValueRtn = ser.Code(Value);
        // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
        maps.put("state",ValueRtn.getState());
        return maps;
    }


    @Override
    public BillTemp getBillTemp(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetBillTemp");
        Value.setUID((String) params.get("uid"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType("AppSalesOrder");
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        BillTemp billTemp=_gson.fromJson(ValueRtn.getJsonObject(),BillTemp.class);
        return billTemp;
    }

    private BillTemp getBillTempX05(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetBillTemp");
        Value.setUID((String) params.get("uid"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType("AppSalesOrder");
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        BillTemp billTemp=_gson.fromJson(ValueRtn.getJsonObject(),BillTemp.class);
        return billTemp;
    }

    /**
     * 获得number
     */
    @Override
    public Map getNumber(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetNumber");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("classtype", "Sales");
        Value.getData().setData("organizeid", ((Integer)params.get("organizeId")));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        String ValueRtn = ser.Code2(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn, new TypeToken<Map<String, Object>>() {
        }.getType());

        return maps;
    }



    /**
     * 获得number
     */
    public Map getNumberX05(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetNumber");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("classtype", "AppSalesOrder");
        Value.getData().setData("organizeid", ((Integer)params.get("organizeId")));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        String ValueRtn = ser.Code2(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn, new TypeToken<Map<String, Object>>() {
        }.getType());

        return maps;
    }


    //获取物料列表
    public Map getSalesItem(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X20");//todo
        Value.setMethod("W_GetSalesItem");//W_GetSalesEmpowerList M_GetSalesTaskCar
        Value.setUID((String) params.get("uid"));

        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
          List list= Utils.JsonToList(ValueRtn.getJsonObject());
       // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
         maps.put("list",list);
        if(maps==null){
            maps=new HashMap();
        }
        maps.put("state",ValueRtn.getState());
        return maps;
    }
    //增加授权单
    public Map addTask(Map<String, Object> params) {
        String uid=(String) params.get("uid");
        uid=uid.replaceAll("\"", "");
        params.remove("uid");
        params.put("uid",uid);
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillSaver billSaver=getBillSaver(params);
        billSaver.setCalssType("Sales");
        billSaver.setStatus("New");
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X20");
        Value.setMethod("W_SalesSave");


        Value.setUID(uid);
        System.out.println(params.get("FCustomName"));
        Value.setJsonObject(_gson.toJson(billSaver));
        //Value.setJsonObject("{\"CalssType\":\"Sales\",\"ID\":0,\"Logs\":[],\"NeedSave\":true,\"NewStamp\":\"352a60cf-2dee-456-9aee-acd46ee41e9a\",\"SaveEntrys\":{\"1\":{\"Page\":1,\"Rows\":[{\"DataExecType\":\"Insert\",\"Fields\":{\"439\":{},\"272\":{\"NewValue\":\"\"},\"26\":{\"NewValue\":1.0},\"576\":{\"NewValue\":\"000.004\"},\"19\":{\"NewValue\":\"000017\"},\"112\":{\"NewValue\":\"000.004\"},\"24\":{\"NewValue\":646.0},\"17\":{\"NewValue\":\"6\"},\"21\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"213\":{\"NewValue\":1000.0},\"438\":{}},\"Index\":0,\"RowID\":0}]}},\"SaveItems\":{\"33\":{\"NewValue\":6},\"53\":{},\"1\":{\"NewValue\":\"000167\"},\"462\":{},\"62\":{\"NewValue\":\"\"},\"50\":{},\"6\":{\"NewValue\":\"01.01.002\"},\"56\":{},\"41\":{},\"200\":{},\"575\":{},\"238\":{},\"42\":{},\"60\":{\"NewValue\":0.0},\"58\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"52\":{},\"55\":{},\"46\":{},\"14\":{\"NewValue\":0},\"51\":{},\"7\":{\"NewValue\":0},\"177\":{},\"2\":{},\"8\":{\"NewValue\":1.0},\"44\":{},\"49\":{\"NewValue\":\"00006\"},\"501\":{},\"30\":{\"NewValue\":0},\"5\":{\"NewValue\":\"石灰石\"},\"43\":{},\"47\":{},\"3\":{\"NewValue\":\"鲁254\"},\"4\":{\"NewValue\":\"1\"},\"57\":{},\"461\":{},\"175\":{},\"61\":{\"NewValue\":\" \"},\"59\":{\"NewValue\":\"000.004\"},\"456\":{},\"275\":{},\"176\":{},\"39\":{\"NewValue\":0},\"9\":{},\"10\":{\"NewValue\":0},\"327\":{}},\"Status\":\"New\"}");
        XModeRtn ValueRtn = ser.Code(Value);
        // String ValueRtn3 = ser.Code3(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>() {
        }.getType());
        if(maps==null){
            maps=new HashMap();
        }
        maps.put("state",ValueRtn.getState());
        return maps;
    }


    private BillSaver getBillSaver(Map<String, Object> params)
    {
        BillTemp _BillTemp=getBillTemp(params);
        BillAdpater _Adapter =new BillAdpater(_BillTemp);
        FieldTemp temp=new FieldTemp();
        temp.setNeedSave(true);
        _Adapter.CharStamp=params.get("guid").toString();
        Double valueDouble= (Double) params.get("organizeId");
        Integer valueInt=valueDouble.intValue();
        params.remove("organizeId");
        params.put("organizeId",valueInt);
        _Adapter.Fields.get("FOrganizeID").setValue(params.get("organizeId"));//组织id
        _Adapter.Fields.get("FCancel").setValue(0);//0
        _Adapter.Fields.get("FBillNo").setValue(getBillNo(params));//有
        _Adapter.Fields.get("FPrintTimes").setValue(0);//0
        _Adapter.Fields.get("FCarNo").setValue(params.get("FCarNo"));//车牌号，手填
        _Adapter.Fields.get("FNote").setValue("");//备注remark
        _Adapter.Fields.get("FStep").setValue(0);//0
        _Adapter.Fields.get("FType").setValue(0);//0
        _Adapter.Fields.get("FItemID").setValue(params.get("itemId"));//物料内码
        //_Adapter.Fields.get("FItemErpID").setValue(_BillSaver.getItemID());
        _Adapter.Fields.get("FItemNumber").setValue(params.get("itemNumber"));//編號
        _Adapter.Fields.get("FItemName").setValue(params.get("itemName"));//名称/
        _Adapter.Fields.get("FCustomID").setValue(params.get("FCustomID"));//客户id
        _Adapter.Fields.get("FCustomName").setValue(params.get("FCustomName"));//客户名称
        //_Adapter.Fields.get("FCustomName").setValue(_DataItem.getNote());//��̨
        _Adapter.Fields.get("FQtyPln").setValue(params.get("preload"));//预装量
        _Adapter.Fields.get("FQtyInsHistory").setValue(0);//历史皮重，填0
        _Adapter.Fields.get("FEmpRht").setValue("");//货主名，0006的真实姓名todo
        _Adapter.Fields.get("FLine").setValue(0);//0
        _Adapter.Fields.get("FBatch").setValue("");//  ""
        //_Adapter.Fields.get("FErpID").setValue(_BillSaver.getErpID());
        _Adapter.Fields.get("_FFleetID").setValue("");//  ""
        _Adapter.Fields.get("_FFleetName").setValue("");//  ""
        _Adapter.Fields.get("_FFleetNumber").setValue("");//  ""

        List<SalesOrderEntity> _ListSalesOrderEntity=getListSalesOrderEntity(params);//todo
        for(SalesOrderEntity Value: _ListSalesOrderEntity){
            int index=_Adapter.Entrys.get(1).AddRow();
//            if(index==1){
//                _Adapter.Fields.get("FOrderNo").setValue(Value.getOrderNo());
//                //_Adapter.Fields.get("FErpID").setValue(Value.getOrderNo());
//                _Adapter.Fields.get("FErpNo").setValue(Value.getOrderNo());
//
//            }

            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FOrderID").setValue(Value.getOrderID());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FOrderAssID").setValue(Value.getOrderAssID());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FOrderNumber").setValue(Value.getOrderNo());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustID").setValue(Value.getCustom().getID());
            //_Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustName").setValue(_DataItem.getNote());//��̨
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustName").setValue(Value.getCustom().getName());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustNumber").setValue(Value.getCustom().getNumber());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FPrice").setValue(Value.getPrice());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FBalance").setValue(Value.getBalance());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FAllot").setValue(Value.getDeduct());

        }

        return _Adapter.GetSaveItem();





    }

    private List<SalesOrderEntity> getListSalesOrderEntity(Map<String, Object> params) {
        List<SalesOrderEntity> _ListSalesOrderEntity=new ArrayList<>();
        Object o=params.get("accountResultList");
        List l= (List) o;
        for(Object oo:l) {
            SalesOrderEntity Value = new SalesOrderEntity();
            Map m= (Map) oo;
        Value.setOrderID((String) m.get("ID"));
        Value.setAssID((String) m.get(""));
        Value.getCustom().setID((String) m.get("CustomID"));
        Value.setOrderNo((String) m.get("BillNo"));
        Value.getCustom().setName((String) m.get("CustomName"));
        Value.getCustom().setNumber((String) m.get("CustomNumber"));
        Value.setPrice((Double) m.get("Price"));
        //Value.setBalance((Integer) m.get("balance"));
            Object preload=m.get("preload");
         if(preload!=null)
        Value.setDeduct(Double.parseDouble(preload.toString()));//todo
        _ListSalesOrderEntity.add(Value);
        }
        return _ListSalesOrderEntity;
    }


    private String getBillNo(Map<String, Object> params) {
        Map map=getNumber(params);
        Map b= (Map) map.get("Data");
        Map c= (Map) b.get("Data");
            return  c.get("billno").toString();
        //return  "0000125";

    }

    //作废授权单
    @Override
    public Map cancleTask(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X20");
        Value.setMethod("M_SalesCancle");
        Value.setUID((String) params.get("uid"));
        Double fid=Double.parseDouble((String) params.get("fid"));
        Integer f= fid.intValue();
        Value.getData().setData("fid", f);
        BillGeter billGeter=new BillGeter();
        billGeter.setID(f.toString());
        billGeter.setCalssType("Sales");
        Value.setJsonObject(_gson.toJson(billGeter));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        XModeRtn ValueRtn = ser.Code(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>() {
        }.getType());
        if(maps==null){
            maps=new HashMap();
        }
        maps.put("state",ValueRtn.getState());
        return maps;
    }


    //根据账户id获取账户下的物料
    @Override
    public Map getSalesItemByAccountID(Map<String, Object> params)
    {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X20");//todo
        Value.setMethod("W_GetSalesItemByAccountID");//W_GetSalesEmpowerList M_GetSalesTaskCar
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("fid", (String)params.get("fid"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list= Utils.JsonToList(ValueRtn.getJsonObject());
        // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
        maps.put("list",list);
        maps.put("state",ValueRtn.getState());
//        params.put("ItemID","1");
//        getSalesAccountListByItemId(params);
        return maps;
    }

    @Override
    public Map getAppOrderList(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");//todo
        Value.setMethod("GetAppOrderList");//M_GetSalesAccountList  M_GetSalesOrder
        Value.setUID((String) params.get("uid"));
        Utils.setDoubleToInt(params,"showclose");
        Value.getData().setData("showclose", (Integer) params.get("showclose"));

        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=Utils.JsonToList(ValueRtn.getJsonObject());
        Map map=new HashMap();
        map.put("list",list);
        map.put("state",ValueRtn.getState());
        return map;
    }

    @Override//todo
    public Map saveBillX05(Map<String, Object> params) {
        String uid=(String) params.get("uid");
        uid=uid.replaceAll("\"", "");
        params.remove("uid");
        params.put("uid",uid);
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        try{
        BillSaver billSaver=getBillSaverX05(params);
        billSaver.setCalssType("AppSalesOrder");
        billSaver.setStatus("New");
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("SaveBill");
        Value.setUID(uid);
        Value.setJsonObject(_gson.toJson(billSaver));
        //Value.setJsonObject("{\"CalssType\":\"Sales\",\"ID\":0,\"Logs\":[],\"NeedSave\":true,\"NewStamp\":\"352a60cf-2dee-456-9aee-acd46ee41e9a\",\"SaveEntrys\":{\"1\":{\"Page\":1,\"Rows\":[{\"DataExecType\":\"Insert\",\"Fields\":{\"439\":{},\"272\":{\"NewValue\":\"\"},\"26\":{\"NewValue\":1.0},\"576\":{\"NewValue\":\"000.004\"},\"19\":{\"NewValue\":\"000017\"},\"112\":{\"NewValue\":\"000.004\"},\"24\":{\"NewValue\":646.0},\"17\":{\"NewValue\":\"6\"},\"21\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"213\":{\"NewValue\":1000.0},\"438\":{}},\"Index\":0,\"RowID\":0}]}},\"SaveItems\":{\"33\":{\"NewValue\":6},\"53\":{},\"1\":{\"NewValue\":\"000167\"},\"462\":{},\"62\":{\"NewValue\":\"\"},\"50\":{},\"6\":{\"NewValue\":\"01.01.002\"},\"56\":{},\"41\":{},\"200\":{},\"575\":{},\"238\":{},\"42\":{},\"60\":{\"NewValue\":0.0},\"58\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"52\":{},\"55\":{},\"46\":{},\"14\":{\"NewValue\":0},\"51\":{},\"7\":{\"NewValue\":0},\"177\":{},\"2\":{},\"8\":{\"NewValue\":1.0},\"44\":{},\"49\":{\"NewValue\":\"00006\"},\"501\":{},\"30\":{\"NewValue\":0},\"5\":{\"NewValue\":\"石灰石\"},\"43\":{},\"47\":{},\"3\":{\"NewValue\":\"鲁254\"},\"4\":{\"NewValue\":\"1\"},\"57\":{},\"461\":{},\"175\":{},\"61\":{\"NewValue\":\" \"},\"59\":{\"NewValue\":\"000.004\"},\"456\":{},\"275\":{},\"176\":{},\"39\":{\"NewValue\":0},\"9\":{},\"10\":{\"NewValue\":0},\"327\":{}},\"Status\":\"New\"}");
        XModeRtn ValueRtn = ser.Code(Value);
        // String ValueRtn3 = ser.Code3(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>() {
        }.getType());
        if(maps==null){
            maps=new HashMap();
        }
        maps.put("billno",billSaver.getSaveItems());
        maps.put("state",ValueRtn.getState());
            //zcx
            String msg = (String)maps.get("Message");
            Map<String, Object> maps2 = _gson.fromJson(ValueRtn.getMessage(), new TypeToken<Map<String, Object>>(){}.getType());

            maps.put("msg",maps2.get("Message")+msg);

            Double d =(Double)maps.get("ID");
            Integer i=d.intValue();
            maps.remove("ID");
            maps.put("ID",i);
            return maps;

        }catch(Exception e){
            e.printStackTrace();
            Map<String, Object> maps = new HashMap<String, Object>();
            maps.put("state",1);
            maps.put("msg","java运行错误："+e.toString());
            return maps;
        }
    }

    private BillSaver getBillSaverX05(Map<String, Object> params) {
        BillTemp _BillTemp=getBillTempX05(params);
        BillAdpater _Adapter =new BillAdpater(_BillTemp);
        FieldTemp temp=new FieldTemp();
        temp.setNeedSave(true);
        _Adapter.CharStamp=params.get("guid").toString();
        Double valueDouble= (Double) params.get("organizeId");
        Integer valueInt=valueDouble.intValue();
        params.remove("organizeId");
        params.put("organizeId",valueInt);
        _Adapter.Fields.get("FOrderID").setValue(params.get("FOrderID"));
        _Adapter.Fields.get("FBillNo").setValue(getBillNoX05(params));//有
        _Adapter.Fields.get("FOrderAssID").setValue(params.get("FOrderAssID")); //订单辅助ID 和FOrderID一样 都是订单带过来的
        _Adapter.Fields.get("FOrderNumber").setValue(params.get("FOrderNumber"));
        _Adapter.Fields.get("FOrderType").setValue(1);  //暂时=1 以后要根据选择情况调整 todo
        _Adapter.Fields.get("FItemID").setValue(params.get("FItemID"));
        _Adapter.Fields.get("FItemNumber").setValue(params.get("FItemNumber"));
        _Adapter.Fields.get("FItemName").setValue(params.get("FItemName"));
      //  _Adapter.Fields.get("FOrderName").setValue(params.get("FOrderName"));  todo
        _Adapter.Fields.get("FCancel").setValue(0);
        _Adapter.Fields.get("FClosed").setValue(0);

        _Adapter.Fields.get("FOrganizeID").setValue(params.get("organizeId"));
        _Adapter.Fields.get("FCustomID").setValue(params.get("FCustomID"));
        _Adapter.Fields.get("FCustomName").setValue(params.get("FCustomName"));
        _Adapter.Fields.get("FCustomNumber").setValue(params.get("FCustomNumber"));
        _Adapter.Fields.get("FQty").setValue(params.get("FQty"));
        _Adapter.Fields.get("FPrice").setValue(params.get("FPrice"));

        //转换日期格式 否则保存不到数据库 Tue Aug 28 2018 13:07:18 GMT+0800 (中国标准时间)转换成 yyyy-mm-dd hh:mm:ss
//        String strStartIime = ConvertDateTime((String) params.get("FStartTime"));
//        _Adapter.Fields.get("FStartTime").setValue(strStartIime);
//
//        String strEndIime = ConvertDateTime((String) params.get("FEndTime"));
//        _Adapter.Fields.get("FEndTime").setValue(strEndIime);

         _Adapter.Fields.get("FStartTime").setValue(params.get("FStartTime"));
         _Adapter.Fields.get("FEndTime").setValue(params.get("FEndTime"));

        _Adapter.Fields.get("FMultiple").setValue(params.get("FMultiple"));

        return _Adapter.GetSaveItem();
    }

    private String ConvertDateTime(String strTime){

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date startDate = new Date(strTime);
            String strConvertIime = sdf.format(startDate);
            return strConvertIime;

        }catch(Exception e){
            e.printStackTrace();
            return "";
        }
    }
    private Object getBillNoX05(Map<String, Object> params) {
        Map map = getNumberX05(params);
        Map b = (Map) map.get("Data");
        Map c = (Map) b.get("Data");
        return c.get("billno").toString();
    }

    @Override
    public Map getAppOrderBill(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("GetAppOrderBill");
        Value.setUID((String) params.get("uid"));

        Utils.setDoubleToInt(params,"ID");
        Value.getData().setData("id", (Integer)params.get("ID"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
         Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        if(maps==null){
            maps = new HashMap();
        }
        maps.put("state",ValueRtn.getState());
        return maps;
    }

    @Override
    public Map getSalesCarList(Map<String, Object> params){

        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("GetSalesCarList");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("orderbillno",(String)params.get("orderBillNo"));
        Value.getData().setData("starttime",(String)params.get("startTime"));
        Value.getData().setData("endtime",(String)params.get("endTime"));
//        Utils.setDoubleToInt(params,"showclose");
//        Value.getData().setData("showclose", (Integer) params.get("showclose"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=Utils.JsonToList(ValueRtn.getJsonObject());
        Map map=new HashMap();
        map.put("list",list);
        map.put("state",ValueRtn.getState());
        return map;

    }

    @Override
    public Map getQtySumByItem(Map<String, Object> params){

        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("GetQtySumByItem");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("starttime",(String)params.get("startTime"));
        Value.getData().setData("endtime",(String)params.get("endTime"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=Utils.JsonToList(ValueRtn.getJsonObject());
        Map map=new HashMap();
        map.put("list",list);
        map.put("state",ValueRtn.getState());
        return map;

    }

}
